﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Kinect = Windows.Kinect;
using System;
public class GestureBase : MonoBehaviour , IGestureCommonInterface
{
    public GameObject BodySourceManager;
    private BodySourceManager _BodyManager;
    private MovingAverage ValueSmoother = new MovingAverage(8);
    private MovingAverage VelocitySmoother = new MovingAverage(8);

    public AnimationCurve debugCurve;
    public float fValue = 0;
    public float fVelocity = 0;
    public bool bTraced = false;
    public bool bSmoothValue = true;

    public float fMinValue = 2;
    public float fMaxValue = 8;
    public float fMinVelocity = 0;
    public float fMaxVelocity = 18;


    public IGestureHandler Handler
    {
        get;set;
    }
    // Use this for initialization
    void Awake () {
        if (BodySourceManager == null)
        {
            return;
        }
        _BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
    }
	
	// Update is called once per frame
      void Update () {
        if (_BodyManager == null)
        {
            return;
        }
        Kinect.Body[] data = _BodyManager.GetData();
        if (data == null)
        {
            return;
        }

        List<ulong> trackedIds = new List<ulong>();
        foreach (var body in data)
        {
            if (body == null)
            {
                continue;
            }

            if (body.IsTracked)
            {
                trackedIds.Add(body.TrackingId);
            }
        }
        foreach (var body in data)
        {
            if (body == null)
            {
                continue;
            }

            if (body.IsTracked)
            {
                if (IsInGesture(body))
                {
                    
                    fValue = GetValue(body);
                    if (bSmoothValue)
                    {
                        fValue = ValueSmoother.SmoothValue(fValue);
                        
                    }
                       
                    bTraced = true;
                    OnTrace(body);
                }
                else
                {
                    bTraced = false;
                    OnNoTrace(body);
                }
                float prevValue = ValueSmoother.GetPreValue();
                fVelocity = Mathf.Abs((fValue - prevValue) / Time.smoothDeltaTime);
                fVelocity = VelocitySmoother.SmoothValue(fVelocity);

            }
        }
    }
   
    /// <summary>
    /// empty implementation for IGestureCommonInterface
    /// </summary>
    /// <param name="body"></param>

    public virtual void OnTrace(Kinect.Body body)
    {
        throw new NotImplementedException();
    }

    public virtual void OnNoTrace(Kinect.Body body)
    {
        throw new NotImplementedException();
    }

    public virtual void Destroy()
    {
        throw new NotImplementedException();
    }

    public virtual bool IsInGesture(Kinect.Body body)
    {
        throw new NotImplementedException();
    }
    public virtual float GetValue(Kinect.Body body)
    {
        throw new NotImplementedException();
    }
}
